package com.uns1066.unsbar.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.uns1066.unsbar.common.CommonConstant;

public class CaptureImage {
	private static final String FFMPEG_PATH = CommonConstant.getffmpegPath() + "ffmpeg/ffmpeg";

	private static VideoInfo getVideoInfo(String filePath) {
		try {
			List command = new ArrayList();
			command.add(FFMPEG_PATH);
			command.add("-i");
			command.add(filePath);
			ProcessBuilder builder = new ProcessBuilder(new String());
			builder.command(command);
			builder.redirectErrorStream(true);
			Process p = builder.start();
			BufferedReader buf = null;
			String line = null;
			buf = new BufferedReader(new InputStreamReader(p.getInputStream()));
			StringBuffer sb = new StringBuffer();
			while ((line = buf.readLine()) != null) {
				System.out.println(line);
				sb.append(line);
			}
			int ret = p.waitFor();
			String[] str = sb.toString().split(",");
			String tempStr = null;
			String rotateStr = null;
			int i = 0;
			for (; i < str.length; i++) {
				if (str[i].trim().length() <= 30 && str[i].contains("x")) {
					if ((SpringUtil.matchNumber((str[i].split("x")[0]).trim()) && SpringUtil.matchNumber((str[i].split("x")[1].trim())))
							|| (SpringUtil.matchNumber((str[i].split("x")[0]).trim()) && str[i].split("x")[1].trim().contains("SAR"))) {
						tempStr = str[i].trim();
						if (str[i].split("x")[1].trim().contains("SAR")) {
							tempStr = tempStr.substring(0, tempStr.indexOf("[")).trim();
						}
						System.out.println(tempStr);
						break;
					}
				}
				if (str[i].indexOf("rotate") != -1) {
					rotateStr = str[i];
				}
			}
			if (rotateStr == null) {
				for (; i < str.length; i++) {
					if (str[i].indexOf("rotate") != -1) {
						rotateStr = str[i];
						break;
					}
				}
			}
			if (tempStr == null)
				return null;
			int start = tempStr.lastIndexOf("x");
			String wStr = tempStr.substring(0, start);
			String hStr = tempStr.substring(start + 1);
			VideoInfo videoInfo = new VideoInfo();
			System.out.println("rotateStr===:" + rotateStr);
			int rotate = getVideoRotate(rotateStr);
			System.out.println("rotate===:" + rotate);
			videoInfo.height = Integer.parseInt(hStr);
			videoInfo.width = Integer.parseInt(wStr);

			// 有旋转角度
			if (rotate == 90 || rotate == 270) {
				int tmp = videoInfo.height;
				videoInfo.height = videoInfo.width;
				videoInfo.width = tmp;
			}
			return videoInfo;
		} catch (Exception e) {
			System.out.println(e);
		}
		return null;
	}

	public static int getVideoRotate(String tempStr) {
		if (tempStr == null)
			return 0;

		int start = tempStr.indexOf("rotate");
		if (start == -1) {
			return 0; // 没有角度
		}

		String sStr = tempStr.substring(start + 6, tempStr.length());
		boolean bFined = false;
		int i = 0;
		for (; i < sStr.length(); i++) {
			if (sStr.charAt(i) == ':') {
				bFined = true;
				break;
			}
		}
		i++;
		if (!bFined)
			return 0;

		bFined = false;
		for (; i < sStr.length(); i++) {
			if (sStr.charAt(i) != ' ') {
				bFined = true;
				break;
			}
		}

		if (!bFined)
			return 0;

		char fNum = sStr.charAt(i);
		if (fNum < '0' || fNum > '9')
			return 0;

		String strNum = "";
		for (; i < sStr.length(); i++) {
			fNum = sStr.charAt(i);
			if (fNum < '0' || fNum > '9') {
				break;
			}
			strNum += fNum;
		}
		int nRotate = Integer.valueOf(strNum);

		return nRotate;
	}

	public static boolean processImg(String filePath, String imgPath) {
		VideoInfo videoInfo = getVideoInfo(filePath);
		if (videoInfo == null)
			return false;
		int h;
		int w;
		if (videoInfo.width > videoInfo.height) {
			w = CommonConstant.getFileWidth();
			float t = (float) (videoInfo.height * 1.0D / (videoInfo.width * 1.0D));
			h = (int) (CommonConstant.getFileHeight() * t);
		} else {
			h = CommonConstant.getFileHeight();
			float t = (float) (videoInfo.width * 1.0D / (videoInfo.height * 1.0D));
			w = (int) (CommonConstant.getFileWidth() * t);
		}
		return processImg(filePath, imgPath, w, h);
	}

	public static boolean processImg(String filePath, String imgPath, int width, int height) {
		File file = new File(filePath);
		if (!file.exists()) {
			System.err.println("路径[" + filePath + "]对应的视频文件不存在!");
			return false;
		}
		List commands = new ArrayList();
		commands.add(FFMPEG_PATH);
		commands.add("-y");
		commands.add("-i");
		commands.add(filePath);
		commands.add("-s");
		commands.add(Integer.toString(width) + "x" + Integer.toString(height));
		commands.add("-f");
		commands.add("image2");
		commands.add(imgPath);
		Process p = null;
		try {
			ProcessBuilder builder = new ProcessBuilder(new String());
			builder.command(commands);
			System.out.println(commands.toString());
			p = builder.start();
			System.out.println("true");
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("false");
		}
		return false;
	}

	public static class VideoInfo {
		public int height;
		public int width;
	}
}